package com.yx.changdao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.entity.*;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.mapper.GridInfoMapper;
import com.yx.changdao.common.vo.BuildBuildVo;
import com.yx.changdao.service.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: <br/>
 * 网格信息库表
 * <p>
 * <br/>
 * @Auther: flexible
 * @create 2020/04/02 13:47
 */
@Service
public class GridInfoServiceImpl extends ServiceImpl<GridInfoMapper, GridInfo> implements GridInfoService {
    /**
     * 网格信息库表服务
     */
    @Autowired
    private GridInfoService gridInfoService;
    /**
     * 社区建筑物表服务
     */
    @Autowired
    private CommunityBuildService communityBuildService;
    /**
     * 户的服务
     */
    @Autowired
    private CommunityBuildDetailService communityBuildDetailService;
    @Autowired
    private CommunityResidentService communityResidentService;


    @Override
    public boolean removeRegion(Integer id) throws AlertException {

        //房id
        List<Integer> buildId = new ArrayList<>();
        List<CommunityBuild> communityBuild = communityBuildService
                .list(new QueryWrapper<CommunityBuild>().eq(CommunityBuild.GRID_ID, id));
        if(communityBuild.size()==0){
            return true;
        }
        for (CommunityBuild build : communityBuild) {
            buildId.add(build.getId());
        }
        //户id
        List<Integer> buildDetailId = new ArrayList<>();
        List<CommunityBuildDetail> buildDetails = communityBuildDetailService.list
                (new QueryWrapper<CommunityBuildDetail>().in(CommunityBuildDetail.BUILD_ID, buildId));
        if(buildDetails.size()==0){
            //户为空，删除房建筑
            //删除楼
            boolean buildRemove = communityBuildService.remove
                    (new QueryWrapper<CommunityBuild>().in(CommunityBuild.ID, buildId));
            if (!buildRemove) {
                return  false;
            }
            return true;

        }
        for (CommunityBuildDetail buildDetail : buildDetails) {
            buildDetailId.add(buildDetail.getId());
        }
        //居民id
        List<CommunityResident> residents = communityResidentService.list
                (new QueryWrapper<CommunityResident>().in(CommunityResident.BUILD_DETAIL_ID, buildDetailId));
        for (CommunityResident integer : residents) {
            integer.setBuildDetailId(0);
            //更新居民
            communityResidentService.updateById(integer);
        }
        //删除户
        boolean buildDetailRemove = communityBuildDetailService.remove
                (new QueryWrapper<CommunityBuildDetail>().in(CommunityBuildDetail.ID, buildDetailId));
        if (!buildDetailRemove) {
           return false;
        }
        //删除楼
        boolean buildRemove = communityBuildService.remove
                (new QueryWrapper<CommunityBuild>().in(CommunityBuild.ID, buildId));
        if (!buildRemove) {
            return  false;
        }
        return true;
    }


}
